package com.foreveredu.propms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ChargeWay;
import com.foreveredu.propms.domain.ChargeWay;
import com.foreveredu.propms.domain.ChargeWay;
import com.foreveredu.propms.domain.ChargeWay;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.web.struts.actionform.ChargeWayActionForm;
import com.foreveredu.propms.web.struts.actionform.ChargeWayActionForm;
import com.foreveredu.propms.web.struts.actionform.ChargeWayActionForm;
import com.foreveredu.propms.web.struts.actionform.ChargeWayActionForm;

public class ChargeWayAction extends AbstractBaseDispatchAction {

	private IChargeService chargeService = null;

	public IChargeService getChargeService() {
		return chargeService;
	}

	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<ChargeWay> chargeWayList = chargeService.findChargeWays();
		request.setAttribute("chargeWayList", chargeWayList);

		return mapping.findForward("chargeWayList");
	}

	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChargeWayActionForm cwf = (ChargeWayActionForm) form;
		ChargeWay ChargeWay = new ChargeWay();
		PropertyUtils.copyProperties(ChargeWay, cwf);
		if (cwf.getName() == null || "".equals(cwf.getName())) {
			request.setAttribute("ChargeWay", ChargeWay);
			request.setAttribute("error", "收费方式不能为空！");
			return mapping.findForward("tochargeWayList");
		}
		chargeService.saveChargeWay(ChargeWay);
		return mapping.findForward("tochargeWayList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChargeWayActionForm cwf = (ChargeWayActionForm) form;
		ChargeWay ChargeWay = chargeService.findChargeWayById(cwf.getId());
		request.setAttribute("ChargeWay", ChargeWay);

		return mapping.findForward("chargeWayUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ChargeWayActionForm cwf = (ChargeWayActionForm) form;
		ChargeWay ChargeWay = chargeService.findChargeWayById(cwf.getId());
		ChargeWay.setName(cwf.getName());
		if(cwf.getName()==null || "".equals(cwf.getName()) ){
			request.setAttribute("ChargeWay", ChargeWay);
			request.setAttribute("error","收费方式不能为空！");
			return mapping.findForward("chargeWayUpdate");
		}
		chargeService.updateChargeWay(ChargeWay);
		return mapping.findForward("update");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChargeWayActionForm cwf = (ChargeWayActionForm) form;
		chargeService.deleteChargeWayById(cwf.getId());
		return mapping.findForward("delete");
	}

}
